######################################################################
#
# D'Bridge Plain-text Help to HTML
# by Lars Kellogg-Stedman 1:322/761
#
# I put this together just to make my life easier while reading
# through the D'Bridge documentation.  It was never meant for
# public consumption, so be nice to it.

BEGIN {
	# Wrap everything in a giant <pre> tag.
	print "<pre>";

	# And put the title parts in a <div> in case we want to 
	# style it someday.
	print "<div id=\"preamble\">";
}

# Basic HTML entity substitution.
{
	gsub("&", "\\&amp;");
	gsub("<", "\\&lt;");
	gsub("", "");
}

######################################################################
# COLLAPSE BLANK LINES
#
# This may not work as well as I thought it did...

/^ *$/ && last_line_was_blank {
	next;
}

/^ *$/ {
	last_line_was_blank = 1;
}

! /^ *$/ {
	last_line_was_blank = 0;
}

######################################################################
# TITLE

/\* USER'S MANUAL \*/ {
	print "</div>";
	print "<h1>User's Manual</h1>";
	next;
}

######################################################################
# TABLE OF CONTENTS

# Time to start generating the TOC entries...
! in_toc && /TABLE OF CONTENTS/ {
	in_toc = 1;
	printf("<a name=\"toc\"/><h2>%s</h2>\n", $0);
	print "<ol>"
	next;
}

in_toc && /TABLE OF CONTENTS/ {
	next;
}

# This means we've reached the end of the TOC.
in_toc && /^0.00/ {
	in_toc = 0;
	if (in_subsection)
		print "</ol></li>"
	print "</ol>"
}

# Discard headings...
in_toc && /Section.*Title/ {
	next;
}

# ...and lines of dashes.
in_toc && /-------/ {
	next;
}

# This recognizes actual TOC entries.
in_toc && /^ +[A-Z0-9]\.[0-9]+ +/ {
	sn = substr($0, 16, 4);
	title = substr($0, 26, 40);
	gsub(" *$", "", title);

	if (sn ~ /\.00$/) {
		if (in_subsection)
			print "</ol></li>"
	}

	printf("<li><a href=\"#%s\">%s %s</a>",
	       sn, sn, title);

	if (sn ~ /\.00$/) {
		print "<!-- START SUBSECTION", sn, "-->"
		printf("\n<ol>\n");
		in_subsection=1
	} else {
		printf("</li>\n");
	}
}

in_toc { next; }

######################################################################
# PAGE BREAK HANDLING
#
# Look for two lines of dashes within three lines of each
# other and get rid of both the lines and everything in between.

! scanning && /^----------/ {
	scanning=1;
	lcount=0;
	delete lbuf;
	next;
}

scanning && /^----------/ {
	scanning=0;
	next;
}

# If we've gone more than three lines without finding another lines of
# dashes, it wasn't really the start of a page separator so we need to
# regurgitate the intervening lines.
scanning && lcount >= 2 {
	print "<hr/>";
	for (i in lbuf) {
		print lbuf[i];
	}
	scanning = 0;
}

scanning {
	lbuf[lcount]=$0;
	lcount++;
	next;
}

######################################################################
# SECTION HEADINGS

# Find main headings.
/^[A-Z0-9]\.[0-9]+/ {
	printf("<a name=\"%s\"/><h2><a href=\"#toc\">%s</a></h2>\n", $1, $0);
	next;
}

# Find subheadings.
/^[[:upper:]][[:upper:] /-]+$/ {
	printf("<h3><a href=\"#toc\">%s</a></h3>\n", $0);
	next;
}
######################################################################
# INLINE MARKUP

# Try to make text * LIKE THIS * a little prettier.
/\* [[:upper:]]+ \*/ {
	$0 = gensub(/\* ([[:upper:]]+) \*/,
	       "* <span style=\"color: red\">\\1</span> *",
	       "",
	       $0)
}


######################################################################
# EVERYTHING ELSE

# Print everything else verbatim.
{
	print;
}

END {
	print "</pre>";
}

